package com.example.fresh_market.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.fresh_market.adapter.UserListAdapter;
import com.example.fresh_market.databinding.ActivityUserManageBinding;
import com.example.fresh_market.entity.UserDO;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.request.PageResult;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class UserManageActivity extends AppCompatActivity {

    private final int requestCode = 12;
    private ActivityUserManageBinding binding;
    private Context mContext;
    private UserListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityUserManageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = this;
        adapter = new UserListAdapter(mContext, new UserListAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(View view, int position) {
                UserDO userDO = adapter.getItem(position);
                Intent intent = new Intent(mContext, UserEditActivity.class);
                intent.putExtra("userId", userDO.getId());
                startActivityForResult(intent, requestCode);
            }

            @Override
            public void onDeleteClick(View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("删除用户");
                builder.setMessage("确定删除该用户吗");
                builder.setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                });
                builder.setPositiveButton("确定", (dialog, which) -> {
                    UserDO userDO = adapter.getItem(position);
                    String delUrl = HttpUtil.getBatchDelUrl("/admin/user", List.of(userDO.getId()));
                    HttpUtil.delete(mContext, delUrl, response -> {
                        ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                        if (apiResponse.getCode() == 200) {
                            ToastUtil.showToast(mContext, "删除成功");
                            init();
                        }
                    });
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }

            @Override
            public void onStatusClick(View view, int position) {
                UserDO userDO = adapter.getItem(position);
                HttpUtil.put(mContext, "/admin/user/status/" + userDO.getId(), null, response -> {
                    ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                    if (apiResponse.getCode() == 200) {
                        init();
                    }
                });
            }
        });
        RecyclerView productList = binding.userList;
        productList.setLayoutManager(new LinearLayoutManager(mContext));
        productList.setAdapter(adapter);
        init();
        productList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {

            }
        });
        binding.addUser.setOnClickListener(view -> {
            startActivityForResult(new Intent(mContext, UserEditActivity.class), requestCode);
        });
    }

    private void init() {
        HttpUtil.get(mContext, "/admin/user?page=1&size=100000", response -> {
            Gson gson = new Gson();
            ApiResponse<PageResult<UserDO>> apiResponse = gson.fromJson(response, new TypeToken<ApiResponse<PageResult<UserDO>>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                adapter.submitList(apiResponse.getData().getList());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == this.requestCode && resultCode == RESULT_OK) {
            init();
        }
    }
}
